<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<title>用户登录-欢聚网</title>
	<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<link rel="stylesheet" href="../css/common/mui.min.css">
	<link rel="stylesheet" href="../css/common/mui.extra.css">
	<link rel="stylesheet" href="../css/common/common.css">
	<style>
		.mui-input-group:before,
		.mui-input-group:after,
		.mui-input-group .mui-input-row:after,
		.mui-table-view:before,
		.mui-table-view:after {
			background: none !important;
		}
	</style>
</head>

<body class="cj-bg-light-grey">
	<div id="writeInfo" style="display:none;">
		<header class="mui-bar mui-bar-nav mui-white-nav-bar">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">填写信息</h1>
		</header>
		<div class="cj-line-20"></div>
		<div class="mui-content cj-bg-light-grey">
			<p class="mui-text-center">首次登陆，请填写完整信息</p>
			<form class="mui-input-group">

				<div class="mui-input-row">
					<input  name="username" type="email" class="mui-input-clear mui-input" placeholder="输入您的手机号">
				</div>
				<div class="mui-input-row">
					<input  name="password" type="password" class="mui-input-clear mui-input" placeholder="输入您的密码">
				</div>
				<div class="mui-input-row">
					<input  name="repassword" type="password" class="mui-input-clear mui-input" placeholder="确认密码">
				</div>
				<div class="mui-input-row">
					<input name="nickname" type="text" class="mui-input-clear mui-input" placeholder="输入您的姓名">
				</div>
				<!-- <div class="mui-input-row">
					<input name="Phones"  type="text" class="mui-input-clear mui-input" placeholder="输入您的手机号">
				</div> -->
				<input name="state" value="success" type="hidden" class="mui-input-clear mui-input" placeholder="">
			</form>
			<div class="cj-line-10"></div>
			<div class="mui-content-padded">
				<button id='reg' data-loading-text="绑定中" class="mui-btn mui-btn-block mui-btn-primary cj-btn">绑定信息</button>
			</div>
		</div>
	</div>
	<div id="wait" >
		<p style="margin-top:80px;text-align:center;">
			<span class="mui-spinner"></span>
		</p>
		<p style="text-align:center;">
			正在登陆...
		</p>
	</div>

	<script src="../js/common/mui.min.js"></script>
	<script src="../js/common/mui.href.js"></script>
	<script src="../js/common/jquery.min.js"></script>
	<script src="../js/common/common.js"></script>
	<script>
	var getUserInfoUrl = "/weixin/getUserDate?code="+getUrlParam("code");
	var openid="";
	//o6WaN1JVl8N3zjJYojAp5WK-j5ZU
	$.ajax({
		url:getUserInfoUrl,
		dataType:"json",
		success:function(res){
			openid = "o6WaN1JVl8N3zjJYojAp5WK-j5ZU";
			$.ajax({
				url:"/user/WeiXinUserMessage?openid="+openid,
				// url:"/user/WeiXinUserMessage?openid="+openid,
				dataType:"json",
				success:function(res){
					if(res.code==200){

						  $.ajax({
						    url: "/login",
						    data:{
									username:res.result.username,
									password:res.result.password,
								},
						    type: 'post',
						    dataType: 'json',
						    success: function(response) {
						      if (response.code == 500) {
						        mui.toast(response.msg);
						      } else {

						        if (typeof(response.result) == "undefined") {
											if(localStorage.goUrl==null || localStorage.goUrl==""){
												location.href = "/user";
											}else{
												location.href = localStorage.goUrl;
											}
						        } else {
						          location.href = response.result;
						        }
						      }
						    }
						  });
					}

				},
				error:function(res){
					if(res.responseJSON.code==500){
						$("#wait").hide();
						$("#writeInfo").show();


					}
				},
			});
		}
	});
	$("#reg").click(function() {
	  //检查邮箱
	  var username = $("input[name='username']").val();
	  var phoneRegx = /^1\d{10}$/;
	  // var emailRegx = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
	  if (phoneRegx.test(username)) {
	    var res;
	    $.ajax({
	      'url': '/user/userNameIsNo?userName=' + username,
	      'dataType': 'json',
	      'async': false,
	      'success': function(response) {
	        res = response;
	      }
	    });
	    if (res.code === 500) {
	      mui.toast(res.msg);
	      return;
	    }

	  } else {
	    mui.toast("请输入正确的手机号");
	    return;
	  }
	  //检查密码
	  var password = $("input[name='password']").val();
	  if (password == null || password == "") {
	    mui.toast("请输入密码");
	    return;
	  }

	  if (password.length < 6) {
	    mui.toast("密码长度不能小于6位");
	    return;
	  }

	  var rePassword = $("input[name='repassword']").val();
	  if (password != rePassword) {
	    mui.toast("两次密码不一致");
	    return;
	  }

	  var name = $("input[name='nickname']").val();
	  if (name == null || name == "") {
	    mui.toast("请输入姓名");
	    return;
	  }

	  // var phoneRegx = /^1\d{10}$/;
	  // var phone= $("input[name='Phones']").val();
	  // if(!phoneRegx.test(phone)){
	  //   mui.toast("请输入正确的手机号");
	  //   return;
	  // }

	  //注册
	  $.ajax({
	    url: "/user/weixinAddUser?nickname="+name+"&username="+username+"&password="+password+"&openid="+openid,
	    type: 'get',
	    dataType: 'json',
	    beforeSend: function() {
	      mui("#reg").button('loading');
	    },
	    complate:function(res){

	    },
	    success: function(response) {
				$.ajax({
					url: "/login",
					data:{
						username:username,
						password:password,
					},
					type: 'post',
					dataType: 'json',
					success: function(response) {
						if (response.code == 500) {
						  mui.toast(response.msg);
						} else {

						  if (typeof(response.result) == "undefined") {
						    location.href = "/user"
						  } else {
						    location.href = response.result;
						  }
						}
					}
				});
				mui("#reg").button('reset');
	    },
	    error:function(res){
	      mui.toast("绑定失败，请重试");
	      mui("#reg").button('reset');
	    }
	  });
	});
	</script>
</body>

</html>
